# 字符串处理的函数
# by lw 2023-11-5

import re


# region 字符串预处理函数：
def str_strip(s: str) -> str:
    """过滤字符串前后的空格、换行符
    Args:
        s (str): 要处理的字符串

    Returns:
        str: 处理后的字符串
    """
    return s.strip()


def str_lstrip(s: str, pattern: re = r'0123456789.- ') -> str:
    """过滤字符串前导的数字编号

    Args:
        s (str): 要处理的字符串
        pattern (re):使用的正则表达式

    Returns:
        str: 处理后的字符串
    """
    return s.lstrip('0123456789.- ')


def str_remove_htmlflag(s: str, pattern: re = r'<[^>]+>') -> str:
    """删除字符串中可能的html标签

    Args:
        s (str): 要处理的字符串
        pattern (re):使用的正则表达式，默认值所有的标签

    Returns:
        str: 处理后的字符串
    """
    return re.sub('<[^>]+>', '', s)


# endregion


# region 字符串分析提取函数：
def segment_split(s: str, pattern: re = r"\s*[一二三四五六七八九]、\s*") -> [str]:
    """使用中文编号分段规则将内容分段

    Args:
        s (str): 要分段的字符串
        pattern (re, optional): 正则表达式，默认值：r"\s*[一二三四五六七八九]、\s*".

    Returns:
        [str]: 分段后的字符串数组
    """
    return re.split(pattern, s)


def get_short_phonenum(str_phonenum: str) -> str:
    """
    从文本中提取8位电话号码
        文本示例：
        text1 = "028-87036212"
        text2 = "电话：028-87036212"
        text3 = "87036212"
        
    Args:
        str_phonenum (str):包含电话号码的文本

    Returns:
        str: 电话号码
    """
    pattern = r"(?:\D|^)(\d{8})(?:\D|$)"
    match = re.search(pattern, str_phonenum)
    if match and match.lastindex > 0:
        return match.group(1)
    else:
        return None


def get_full_phonenum(str_phonenum: str) -> str:
    """
    从文本中提取含区号的电话号码
        文本示例：
        text1 = "028-87036212"
        text2 = "电话：028-87036212"
        text3 = "87036212"
        
    Args:
        str_phonenum (str): 包含电话号码的文本

    Returns:
        str: 含区号的电话号码
    """
    pattern = r"(?:\D|^)(\d{3}-?\d{8,11})(?:\D|$)"
    match = re.search(pattern, str_phonenum)
    if match and match.lastindex > 0:
        return match.group(1)
    else:
        return None


def get_mobile_phonenum(str_phonenum: str) -> str:
    """
    从文本中提取手机号码
        文本示例：
        text1 = "联系方式：13812345678"
        text2 = "13812345678"
        
    Args:
        str_phonenum (str): 包含电话号码的文本

    Returns:
        str: 手机号码
    """
    pattern = r"(?:\D|^)(?:\+86)?1[3-9]\d{9}(?:\D|$)"
    match = re.search(pattern, str_phonenum)
    if match:
        return match.group(0)
    else:
        return None


# endregion


# region 格式检查类函数：
def check_url(url: str) -> bool:
    """检查输入的字符串是否是合法的url

    Args:
        url (str): url字符串

    Returns:
        bool: 是合法的url返回True，不是返回False
    """
    # 使用正则表达式检查URL格式
    pattern = re.compile(r'^https?://[^\s/$.?#].[^\s]*$')
    if re.match(pattern, url):
        return True
    else:
        return False


# endregion

# 示例用法
if __name__ == '__main__':
    url = "http://www.example.com?abnc=123/34"
    if check_url(url):
        print("URL is valid")
    else:
        print("URL is invalid")

    # 电话号码类函数测试
    text1 = "028-87036212"
    text2 = "电话：028-87036212"
    text3 = "87036212"
    text4 = "788987036212"
    text5 = "7ian adsfd 12"
    text6 = "手机：18507165899"
    text7 = "手机：  18507165899"

    print("电话号码1短号:", get_short_phonenum(text1))
    print("电话号码1含区号:", get_full_phonenum(text1))
    print("电话号码2短号:", get_short_phonenum(text2))
    print("电话号码2含区号:", get_full_phonenum(text2))
    print("电话号码3短号:", get_short_phonenum(text3))
    print("电话号码3含区号:", get_full_phonenum(text3))
    print("电话号码4短号:", get_short_phonenum(text4))
    print("电话号码4含区号:", get_full_phonenum(text4))
    print("电话号码5短号:", get_short_phonenum(text5))
    print("电话号码5含区号:", get_full_phonenum(text5))

    print("手机号6:", get_mobile_phonenum(text6))
    print("手机号7:", get_mobile_phonenum(text7))
